package com.vegetable.modules.controller.allocation;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.vegetable.common.annotation.SysLog;
import com.vegetable.common.utils.R;
import com.vegetable.modules.controller.AbstractController;
import com.vegetable.modules.entity.allocation.AllocationCollection;
import com.vegetable.modules.entity.allocation.po.AllocationCollectionAddPO;
import com.vegetable.modules.entity.allocation.po.AllocationCollectionQueryDTO;
import com.vegetable.modules.entity.allocation.po.CollectionDetailsQueryDTO;
import com.vegetable.modules.entity.allocation.po.DeductionStatisticsQueryDTO;
import com.vegetable.modules.entity.allocation.po.UpdateDeductionHandleOpinionPO;
import com.vegetable.modules.entity.allocation.po.UpdateDeductionHandleOpinionWithDetailsPO;
import com.vegetable.modules.entity.allocation.vo.DeductionProcessResultVO;
import com.vegetable.modules.entity.allocation.vo.DeductionStatisticsVO;
import com.vegetable.modules.entity.allocation.vo.SalesOrderCollectionVO;
import com.vegetable.modules.service.allocation.IAllocationCollectionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 分配回款 前端控制器
 * </p>
 *
 * @author Qoder
 * @since 2025-10-08
 */
@Slf4j
@RestController
@RequestMapping("/allocation/collection")
@Api(value = "分配回款", tags = "分配回款")
public class AllocationCollectionController extends AbstractController {
    
    @Autowired
    private IAllocationCollectionService allocationCollectionService;

    /**
     * 分页查询分配回款列表
     */
    @GetMapping("/list")
    @ApiOperation(value = "分页查询分配回款列表", notes = "分页查询分配回款列表")
    public R<IPage<AllocationCollection>> list(AllocationCollectionQueryDTO queryDTO){
        log.info("分页查询分配回款列表{}", JSON.toJSONString(queryDTO));
        IPage<AllocationCollection> page = allocationCollectionService.queryPage(queryDTO);
        return R.data(page);
    }

    /**
     * 注意：回款记录不能由前端直接创建，只能通过支付流水匹配到客户ID后自动创建
     * 此接口已废弃，仅保留用于内部系统调用
     */
    @Deprecated
    @SysLog(value = "保存分配回款")
    @PostMapping("/save")
    @ApiOperation(value = "保存分配回款", notes = "保存分配回款（注意：此接口仅供内部系统调用，前端不应直接调用）")
    public R save(@RequestBody AllocationCollectionAddPO po) throws Exception {
        log.info("保存分配回款：{}", JSON.toJSONString(po));
        po.setCreateUserId(getUserId());
        return allocationCollectionService.saveAllocationCollection(po);
    }

    /**
     * 分配回款
     */
    @SysLog(value = "分配回款")
    @PostMapping("/allocated")
    @ApiOperation(value = "分配回款", notes = "分配回款")
    public R allocated(@RequestBody AllocationCollectionAddPO po) throws Exception {
        log.info("分配回款：{}", JSON.toJSONString(po));
        po.setCreateUserId(getUserId());
        return allocationCollectionService.allocatedCollection(po);
    }

    /**
     * 分配回款详情
     */
    @SysLog(value = "分配回款详情")
    @GetMapping("/detail")
    @ApiOperation(value = "根据id查询分配回款详情", notes = "根据id查询分配回款详情")
    public R detail(@RequestParam Long id){
        if (id == null || id <= 0){
            return R.fail("订单主键不能为空");
        }
        log.info("根据id查询分配回款详情：{}", JSON.toJSONString(id));
        return allocationCollectionService.detailCollection(id);
    }

    /**
     * 更新回款完成状态
     */
    @SysLog(value = "更新回款完成状态")
    @PostMapping("/updateCollectionCompleteStatus")
    @ApiOperation(value = "更新回款完成状态", notes = "更新回款完成状态")
    public R updateCollectionCompleteStatus(@RequestParam Long id, @RequestParam Integer status) {
        log.info("更新回款完成状态：id={}, status={}", id, status);
        return allocationCollectionService.updateCollectionCompleteStatus(id, status);
    }

    /**
     * 删除分配回款
     */
    @SysLog(value = "删除分配回款")
    @GetMapping("/delete")
    @ApiOperation(value = "根据id删除分配回款", notes = "根据id删除分配回款")
    public R delete(@RequestParam Long id) throws Exception {
        log.info("删除分配回款：{}", JSON.toJSONString(id));
        return allocationCollectionService.deleteCollection(id);
    }

    /**
     * 回款情况查询接口
     * 查询条件：客户、订单号、订单时间
     */
    @GetMapping("/collectionDetails")
    @ApiOperation(value = "回款情况查询", notes = "根据客户、订单号、订单时间查询回款情况")
    public R<List<SalesOrderCollectionVO>> collectionDetails(CollectionDetailsQueryDTO queryDTO) {
        log.info("回款情况查询：{}", JSON.toJSONString(queryDTO));
        // 这里需要实现具体的查询逻辑
        // 目前先返回空列表，后续需要实现具体的业务逻辑
        List<SalesOrderCollectionVO> result = allocationCollectionService.getCollectionDetailsByConditions(queryDTO);
        return R.data(result);
    }

    /**
     * 统计扣款情况
     */
    @GetMapping("/deductionStatistics")
    @ApiOperation(value = "统计扣款情况", notes = "统计扣款情况")
    public R<List<DeductionStatisticsVO>> deductionStatistics(DeductionStatisticsQueryDTO queryDTO) {
        log.info("统计扣款情况：{}", JSON.toJSONString(queryDTO));
        List<DeductionStatisticsVO> result = allocationCollectionService.getDeductionStatistics(queryDTO);
        return R.data(result);
    }
    
    /**
     * 更新扣款处理意见（包含明细）
     */
    @SysLog(value = "更新扣款处理意见（包含明细）")
    @PostMapping("/updateDeductionHandleOpinionWithDetails")
    @ApiOperation(value = "更新扣款处理意见（包含明细）", notes = "更新扣款处理意见（包含明细）")
    public R updateDeductionHandleOpinionWithDetails(@RequestBody UpdateDeductionHandleOpinionWithDetailsPO po) {
        log.info("更新扣款处理意见（包含明细）：id={}, opinion={}", po.getId(), po.getOpinion());
        return allocationCollectionService.updateDeductionHandleStatusAndOpinionWithDetails(po);
    }
    
    /**
     * 获取销售出库单的走货详情用于扣款处理
     */
    @GetMapping("/getShipmentDetailsForDeduction")
    @ApiOperation(value = "获取销售出库单的走货详情用于扣款处理", notes = "获取销售出库单的走货详情用于扣款处理")
    public R<DeductionProcessResultVO> getShipmentDetailsForDeduction(Long salesOutId) {
        log.info("获取销售出库单的走货详情用于扣款处理：salesOutId={}", salesOutId);
        DeductionProcessResultVO result = allocationCollectionService.getShipmentDetailsForDeduction(salesOutId);
        return R.data(result);
    }
}